<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Brief extends Base_Controller{
	
	private $wx_user;

	public function __construct(){
		parent::__construct();
		$this->load->library('session');
		$this->load->model(['signup_share_count_model', 'signup_share_model']);
		$this->wx_user = $this->session->userdata('wx_user');
	}
	
	public function index(){
		if(!$this->wx_user){
			$this->load->library('Wechat/wechatjs');
			$openid = $this->wechatjs->get_openid();
			$wx_user = $this->wechatjs->get_oauth_userinfo($openid);
			$this->session->set_userdata('wx_user', $wx_user);
			$this->wx_user = $wx_user;
		}
		
		$gets = $this->input->get();
		
		$openid = $this->wx_user['openid'];
		$rand = mt_rand(1, 5);
		
		$count = $this->signup_share_count_model->get_row();
		$this->signup_share_count_model->set([
			'visits' => "visits + $rand",
			'real_visits' => 'real_visits + 1'
		], [
			'id' => 1
		], true);
		
		// 自己是否登记过代言信息
		$signed = $this->signup_share_model->get_row([
			'openid' => $openid
		]);
		if($signed){
			$this->signup_share_model->set([
				'headimg' => $this->wx_user['headimgurl']
			], [
				'openid' => $openid
			]);
			unset($signed['id'], $signed['inputtime']);
		}
		
		// 来自其他人的分享
		$from_openid = $gets['from_openid'];
		if($from_openid){
			$this->signup_share_model->set([
				'visits' => 'visits + 1'
			], [
				'openid' => $from_openid
			], true);
			$frominfo = $this->signup_share_model->get_row([
				'openid' => $from_openid
			]);
		}
		
		$page_data = [
			'visits' => (int)$count['visits'],
			'user' => [
				'openid' => $openid,
				'name' => $this->wx_user['nickname'],
				'headimg' => $this->wx_user['headimgurl']
			],
			'signed' => $signed
		];
		$assign = [
			'page_data' => $page_data,
			'frominfo' => $frominfo,
			'signed' => $signed
		];
		
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('brief_index.htm');
	}
	
	public function submit(){
		$posts = $this->input->post();
		if(!$posts){
			$this->show_message();
		}
		if(!$this->wx_user){
			$this->show_message('请在微信中打开');
		}
		$this->load->library('form_validation');
		$this->form_validation->set_rules('type', '类型', 'required');
		$this->form_validation->set_rules('name', '姓名', 'required');
		$this->form_validation->set_rules('role', '身份', 'required');
		if($this->form_validation->run() == false){
			foreach($this->form_validation->error_array() as $filed => $error){
				$this->output_json([
					'retcode' => -1,
					'message' => $error
				]);
			}
		}
		$row = $this->signup_share_model->get_row([
			'openid' => $this->wx_user['openid']
		]);
		if($row){
			$this->output_json('已经登记过了');
		}
		$from_openid = $posts['from_openid'];
		$count = $this->signup_share_count_model->get_row();
		$position = $count['visits'] + 1;
		$query = $this->signup_share_model->set([
			'openid' => $this->wx_user['openid'],
			'headimg' => $this->wx_user['headimgurl'],
			'from_openid' => $from_openid,
			'type' => $posts['type'],
			'name' => $posts['name'],
			'role' => $posts['role'],
			'position' => $position,
			'inputtime' => time()
		]);
		if($query){
			$this->signup_share_count_model->set([
				'sign_num' => 'sign_num + 1'
			], [
				'id' => 1
			], true);
			$this->signup_share_model->set([
				'childs' => 'childs + 1'
			], [
				'openid' => $from_openid
			], true);
			$this->output_json([
				'message' => '信息登记成功',
				'position' => $position
			], 0);
		}
		$this->output_json('信息登记失败');
	}
	
}